/*
 * MyRightClickMenu.java
 *
 * Created on May 29, 2000, 1:49 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

/**
 *
 * @author ayman
 */
public class MyRightClickMenu {
    
    private JPopupMenu rightClickMenu = new JPopupMenu();
    private MyFrame  parent ;
    private MyComponentContainer comp ;
    private int xPos;
    private int yPos;
    private int selCount;
    private Vector selectedComp = new Vector();
    
    /** Creates a new instance of MyRightClickMenu */
    public MyRightClickMenu(MyComponentContainer comp , int xPos , int yPos , MyFrame parent) {
        this.parent = parent;
        this.xPos = xPos;
        this.yPos = yPos;
        this.comp = comp;
        fillMenu();
    }
    
    
    /**
     *   The function used to fill the pop up menu
     */
    private void fillMenu() {
        
        SelectPopUpmenuHandler handle = new SelectPopUpmenuHandler();
        
        //Creating a copy item
        JPopupMenu popMenu = new JPopupMenu();
        JMenuItem copyItem =new JMenuItem("Copy");
        copyItem.setName("copy");
        copyItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/copy.png")));
        copyItem.addActionListener(handle);
        
        //Creating a cut item
        JMenuItem cutItem =new JMenuItem("Cut");
        cutItem.setIcon(new ImageIcon(getClass().getResource("/Images/cut.png")));
        cutItem.setName("cut");
        cutItem.addActionListener(handle);
        
        //Creating a paste item
        JMenuItem pasteItem =new JMenuItem("Paste");
        pasteItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/paste.png")));
        pasteItem.setName("paste");
        pasteItem.addActionListener(handle);
        
        //Creating a properties item
        JMenuItem propertiesItem =new JMenuItem("Properties");
        propertiesItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/properties.png")));
        propertiesItem.setName("properties");
        propertiesItem.addActionListener(handle);
        
        //Creating a font item
        JMenuItem fontItem =new JMenuItem("Font");
        fontItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/foreColor.png")));
        fontItem.setName("font");
        fontItem.addActionListener(handle);
        
        //Creating a align item
        JMenu alignItem =new JMenu("      Align");
        
        JMenuItem left = new JMenuItem("Left");
        left.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/AlignLeft.png")));
        left.setName("left");
        
        JMenuItem right = new JMenuItem("Right");
        right.setName("right");
        right.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/AlignRight.png")));
        
        JMenuItem center = new JMenuItem("Center");
        center.setName("center");
        center.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/AlignCenter.png")));
        
        left.addActionListener(handle);
        right.addActionListener(handle);
        center.addActionListener(handle);
        
        alignItem.add(right);
        alignItem.add(center);
        alignItem.add(left);
        
        popMenu.add(alignItem);
        //alignItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/align.png")));
        alignItem.setName("align");
        
        
        //Creating a size item
        JMenu sizeItem =new JMenu("      Size");
        JMenuItem narrow = new JMenuItem("To Narrowest");
        narrow.setName("narrow");
        JMenuItem wide = new JMenuItem("To Widest");
        wide.setName("wide");
        JMenuItem tall = new JMenuItem("To Tallest");
        tall.setName("tall");
        JMenuItem toshort = new JMenuItem("To Shortest");
        toshort.setName("short");
        for (MyComponentContainer comp : parent.componentList) {
            if (comp.isChosen()){
                selectedComp.add(comp);
                selCount++;
            }
        }
        if (!(selCount>1)){
            narrow.setEnabled(false);
            wide.setEnabled(false);
            tall.setEnabled(false);
            toshort.setEnabled(false);
        }
        
        
        // Add action listener
        narrow.addActionListener(handle);
        wide.addActionListener(handle);
        tall.addActionListener(handle);
        toshort.addActionListener(handle);
        
        // Add to the size menu
        sizeItem.add(narrow);
        sizeItem.add(wide);
        sizeItem.add(tall);
        sizeItem.add(toshort);
        
        popMenu.add(sizeItem);
        //alignItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/align.png")));
        //sizeItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/.png")));
        
        sizeItem.addActionListener(handle);
        
        
        // Adding item to menu
        popMenu.add(copyItem);
        popMenu.add(cutItem);
        popMenu.add(pasteItem);
        popMenu.addSeparator();
        popMenu.add(fontItem);
        popMenu.add(alignItem);
        popMenu.add(sizeItem);
        popMenu.addSeparator();
        popMenu.add(propertiesItem);
        
        popMenu.show(comp.getComponent() , xPos , yPos);
        //System.out.println("x = " + xPos + " y= " + yPos);
    }
    /**
     *  The class used to handle the right click on the objects
     *
     */
    class SelectPopUpmenuHandler implements ActionListener {
        public void actionPerformed(ActionEvent e){
            String sourceName = ((JMenuItem)(e.getSource())).getName();
            
            // if properties is clicked
            if (sourceName == "properties") {
                MyPropertiesWindow properties = new MyPropertiesWindow(comp,parent);
            }
            
            // if the copy is clicked
            if (sourceName == ("copy")) {
                parent.toolbar.copy();
            }
            // if the cut is clicked
            if (sourceName == "cut") {
                parent.toolbar.cut();
            }
            // if the paste is clicked
            if (sourceName == "paste") {
                parent.toolbar.paste();
            }
            // if the font is clicked
            if (sourceName == "font") {
                int numberSelected=0;
                MyComponentContainer component=null;
                for (MyComponentContainer c : parent.componentList) {
                    if (c.isChosen()) {
                        numberSelected++;
                        component=c;
                    }
                }
                if (numberSelected>1) {
                    Vector<JComponent> compList = new Vector<JComponent> ();
                    for (MyComponentContainer c : parent.componentList) {
                        if (c.isChosen()){
                            compList.add(c.getComponent());
                        }
                    }
                    AdvancedFontChooser.getAndSetFont(compList);
                } else {
                    AdvancedFontChooser.getAndSetFont(component.getComponent()) ;
                }
                parent.handleUndo();
            }
            // if the left is clicked
            if (sourceName == "left") {
                parent.formatToolBar.alignLeft();
                parent.handleUndo();
            }
            // if the right is clicked
            if (sourceName == "right") {
                parent.formatToolBar.alignRight();
                parent.handleUndo();
            }
            // if the center is clicked
            if (sourceName == "center") {
                parent.formatToolBar.alignCenter();
                parent.handleUndo();
            }
            // if the narrow is clicked
            if (sourceName == "narrow") {
                int narrowest=0;
                int temp=0;
                // System.out.println("narrow");
                narrowest = ((MyComponentContainer)selectedComp.elementAt(0)).getComponent().getWidth();
                for (int i=1; i < selectedComp.size() ; i++ ) {
                    temp = ((MyComponentContainer)selectedComp.elementAt(i)).getComponent().getWidth();
                    if ( narrowest > temp ) {
                        narrowest = temp;
                    }
                    //System.out.println(" narr " + narrowest)                                    ;
                }
                for (int i=0; i < selectedComp.size()  ; i ++ ){
                    ((MyComponentContainer)selectedComp.elementAt(i)).getComponent().setSize(narrowest, comp.getComponent().getHeight());
                    ((MyComponentContainer)selectedComp.elementAt(i)).showBorder();
                    ((MyComponentContainer)selectedComp.elementAt(i)).resizePanel();
                }
                parent.handleUndo();
            }
            // if the wide is clicked
            if (sourceName == "wide") {
                //System.out.println("wide");
                int widest=0;
                int temp=0;
                widest = ((MyComponentContainer)selectedComp.elementAt(0)).getComponent().getWidth();
                for (int i=1; i < selectedComp.size() ; i++ ) {
                    temp = ((MyComponentContainer)selectedComp.elementAt(i)).getComponent().getWidth();
                    if ( widest < temp ) {
                        widest = temp;
                    }
                    //System.out.println(" narr " + narrowest)                                    ;
                }
                for (int i=0; i < selectedComp.size()  ; i ++ ){
                    ((MyComponentContainer)selectedComp.elementAt(i)).getComponent().setSize(widest, comp.getComponent().getHeight());
                    ((MyComponentContainer)selectedComp.elementAt(i)).showBorder();
                    ((MyComponentContainer)selectedComp.elementAt(i)).resizePanel();
                }
                parent.handleUndo();
            }
            // if the short is clicked
            if (sourceName == "short") {
                int shortest=0;
                int temp=0;
                shortest = ((MyComponentContainer)selectedComp.elementAt(0)).getComponent().getHeight();
                for (int i=1; i < selectedComp.size() ; i++ ) {
                    temp = ((MyComponentContainer)selectedComp.elementAt(i)).getComponent().getHeight();
                    if ( shortest > temp ) {
                        shortest = temp;
                    }
                    //System.out.println(" narr " + narrowest)                                    ;
                }
                for (int i=0; i < selectedComp.size()  ; i ++ ){
                    ((MyComponentContainer)selectedComp.elementAt(i)).getComponent().setSize(comp.getComponent().getWidth(), shortest);
                    ((MyComponentContainer)selectedComp.elementAt(i)).showBorder();
                    ((MyComponentContainer)selectedComp.elementAt(i)).resizePanel();
                }
                parent.handleUndo();
            }
            // if the tall is clicked
            if (sourceName == "tall") {
                System.out.println("tall");
                int tallest=0;
                int temp=0;
                tallest = ((MyComponentContainer)selectedComp.elementAt(0)).getComponent().getHeight();
                for (int i=1; i < selectedComp.size() ; i++ ) {
                    temp = ((MyComponentContainer)selectedComp.elementAt(i)).getComponent().getHeight();
                    if ( tallest < temp ) {
                        tallest = temp;
                    }
                    //System.out.println(" narr " + narrowest)                                    ;
                }
                for (int i=0; i < selectedComp.size()  ; i ++ ){
                    ((MyComponentContainer)selectedComp.elementAt(i)).getComponent().setSize(comp.getComponent().getWidth(), tallest);
                    ((MyComponentContainer)selectedComp.elementAt(i)).showBorder();
                    ((MyComponentContainer)selectedComp.elementAt(i)).resizePanel();
                }
                parent.handleUndo();
            }
            
        }
    }
    
    
    //#########################################################################
    
    
    /**
     *  The function use is to pop up the right click menu
     */
    private void popUpMenu() {
        //rightClickMenu
    }
    
}
